from optparse import OptionParser
import subprocess, re

usage=" prog [options] -i IP Address or Hostname\n example: python ntp_monlist.py -h www.example.com\n example: python ntp_monlist.py -h 10.10.2.1 -p 80"
desc="This program will help user to test if target IP is vulnerable to NTP monlist Denial of Service Vulnerability by passing target ip and port\n. "
parser = OptionParser(usage=usage,description=desc);
parser.add_option("-i","--ip",help="IP Address",type="string",dest="ipAddress",action="store")
parser.add_option("-p","--port",help="Port number",type="int",dest="port",action="store")

(options,args) = parser.parse_args();
if options.ipAddress is None:
	parser.print_help();
	parser.error('Invalid argument, IP Address is mandatory')
else:
	ipAddress = options.ipAddress
	port =  options.port

 	# ntpcmd = "ntpdc -c monlist"
	p = subprocess.Popen(["ntpdc", "-c", "monlist", ipAddress], stdout=subprocess.PIPE)
	output, err = p.communicate ()
	print output

while True:
	if output == '':
		break
	else:
			# Check if the target is vulnerable or not
		match = re.match (r'(.*)remote address.*', output, re.I)
		if match:
			print "Target appears to be vulnerable to NTP monlist feature Denial of Service Vulnerability"
		else:
			print "Target appears to be patched for NTP monlist feature Denial of Service Vulnerability"
	